package portal.pubcontroller.system;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import portal.pub.util.pf.ResultUtil;

import net.sf.json.JSONObject;
import portal.pub.util.pf.JSONResult;
import portal.pub.util.pf.JsonObjectUtil;
import portal.pub.util.pf.Result;
import portal.pubitf.system.pf.INoticeService;
import portal.pubvo.system.NoticeVo;

@Controller
@SuppressWarnings("all")
public class NoticeController {
	
	@Resource(name = "noticeServiceImpl")
	private INoticeService noticeService;
	
	//查询列表
	@RequestMapping(value = "/system/noticeList")
	public void noticeList(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) {
		Result result = noticeService.getAll();
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("status", "0");
		try {
			response.getWriter().println(result.getJson());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//分页查询列表
	@RequestMapping(value = "/system/noticePageList")
	@ResponseBody
	public ResultUtil noticePageList(Integer page, Integer limit,String title){
		return noticeService.getPageAll(page, limit,title);
	}
	//添加公告
	@RequestMapping(value = "/system/addNotice")
	public void addNotice(@RequestBody NoticeVo vo,HttpServletResponse response) {
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		vo.setId(UUID.randomUUID().toString());
		vo.setCreator("0001");
		vo.setMemo("无");
		System.out.println(vo);
		int count = noticeService.addNotice(vo);
		if(count>0) {
			//添加成功
			json.put("status", "0");
			json.put("message", "添加成功");
		}else {
			//添加失败
			json.put("status", "1");
			json.put("message", "添加失败");
		}
		try {
			response.getWriter().println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//更新公告
	@RequestMapping(value = "/system/editNotice")
	public void editNotice(@RequestBody NoticeVo vo,HttpServletResponse response) {
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		vo.setModifier("0002");
		int count = noticeService.editNotice(vo);
		if(count>0) {
			//修改成功
			json.put("status", "0");
			json.put("message", "修改成功");
		}else {
			//修改失败
			json.put("status", "1");
			json.put("message", "修改失败");
		}
		try {
			response.getWriter().println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//删除公告记录
	@RequestMapping(value = "/system/deleteNotice")
	public void deleteNotice(String id,@RequestBody String[] delList,HttpServletResponse response) {
		System.out.println(delList);
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		int count = noticeService.deleteNotice(id);
		if(count>0) {
			//删除成功
			json.put("status", "0");
			json.put("message", "删除成功");
		}else {
			//删除失败
			json.put("status", "1");
			json.put("message", "删除失败");
		}
		try {
			response.getWriter().println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//删除公告记录
	@RequestMapping(value = "/system/deleteNoticeInId")
	public void deleteNoticeInId(String delStr,HttpServletResponse response) {
		delStr = delStr.substring(0, delStr.length()-1);
		//System.out.println(delStr);
		String[] delArr = delStr.split(",");
		//System.out.println(Arrays.toString(delArr));
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		int count = noticeService.deleteNoticeInId(delArr);
		//System.out.println(count);
		if(count>0) {
			//删除成功
			json.put("status", "0");
			json.put("message", "删除成功");
		}else {
			//删除失败
			json.put("status", "1");
			json.put("message", "删除失败");
		}
		try {
			response.getWriter().println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
